package com.dgz.csmall.product.controller;

import com.dgz.csmall.commons.security.LoginPrincipal;
import com.dgz.csmall.product.pojo.param.AlbumAddNewParam;
import com.dgz.csmall.product.pojo.param.AlbumUpdateInfoParam;
import com.dgz.csmall.product.pojo.vo.AlbumListItemVO;
import com.dgz.csmall.product.pojo.vo.AlbumStandardVO;
import com.dgz.csmall.commons.pojo.vo.PageData;
import com.dgz.csmall.product.service.AlbumService;
import com.dgz.csmall.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

/**
 * @Author duguozheng
 * @Date 2023/05/09  17:52
 * @Description 处理相册相关请求的控制器类
 * @Version 1.0
 */
@RestController
@RequestMapping("/albums/")
@Api(tags = "04. 相册管理模块")
@Slf4j
@Validated
public class AlbumController {

    @Autowired
    private AlbumService albumService;

    public AlbumController() {
        log.debug("创建控制器类对象：AlbumController");
    }

    //http://localhost:9180/albums/add-new
    @PostMapping("add-new")
    @PreAuthorize("hasAuthority('/pms/album/add-new')") //Spring Security注解，只有具有"/pms/album/add-new"权限的用户才能访问这个方法
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid AlbumAddNewParam albumAddNewParam) {
        log.debug("开始处理【添加相册】的请求，参数：{}", albumAddNewParam);
        albumService.addNew(albumAddNewParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/albums/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/pms/album/delete')")
    @ApiOperation("根据ID删除相册")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "根据ID删除相册失败，请提交合法的ID值！")  Long id) {
        log.debug("开始处理【根据ID删除相册】的请求，参数:{}", id);
        albumService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/albums/9527/update
    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/pms/album/update')")
    @ApiOperation("修改相册详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的相册ID值！") long id, @Valid AlbumUpdateInfoParam albumUpdateInfoParam) {
        log.debug("开始处理【修改相册详情】的请求，id：{},新数据：{}", id, albumUpdateInfoParam);
        albumService.updateInfoById(id, albumUpdateInfoParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/albums/9527
    @GetMapping("/{id:[0-9]+}/")
    @PreAuthorize("hasAuthority('/pms/album/read')")
    @ApiOperation("根据ID查询相册详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")
    })
    public JsonResult<AlbumStandardVO> getStandardById(@PathVariable @Range(min = 1, message = "请提交合法的ID值！") Long id) {
        log.debug("开始处理【根据ID查询相册详情】的请求，参数：{}", id);
        AlbumStandardVO result = albumService.getStandardById(id);
        return JsonResult.ok(result);
    }

    // http://localhost:9180/albums
    @GetMapping("")
    @PreAuthorize("hasAuthority('/pms/album/read')")
    @ApiOperation("查询相册列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", defaultValue = "1", dataType = "long"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult<PageData<AlbumListItemVO>> list(@Range(min = 1, message = "请提供正确的页码值！") Integer page, String queryType) {
        log.debug("开始处理【查询相册列表】的请求，页码:{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<AlbumListItemVO> pageData;

        //如果查询类型为“all”，则查询所有数据，即实现查询结果不分页
        if ("all".equals(queryType)) {
            pageData = albumService.list(pageNum, Integer.MAX_VALUE);
        } else {
            pageData = albumService.list(pageNum);
        }

        return JsonResult.ok(pageData);
    }

    // http://localhost:9180/albums/rebuild-cache
    @PostMapping("/rebuild-cache")
    @PreAuthorize("hasAuthority('/pms/album/read')")
    @ApiOperation("重建相册缓存")
    @ApiOperationSupport(order = 900)
    public JsonResult<Void> rebuildCache() {
        log.debug("开始处理【重建相册缓存】的请求，无参数");
        albumService.rebuildCache();
        return JsonResult.ok();
    }



}
